import { defineStore } from 'pinia'

// 境界配置
const REALMS = {
  '练气期': { maxCultivation: 1000, levelBonus: 0 },
  '筑基期': { maxCultivation: 2000, levelBonus: 10 },
  '金丹期': { maxCultivation: 5000, levelBonus: 30 },
  '元婴期': { maxCultivation: 10000, levelBonus: 60 },
  '化神期': { maxCultivation: 20000, levelBonus: 100 },
  '合体期': { maxCultivation: 50000, levelBonus: 150 },
  '大乘期': { maxCultivation: 100000, levelBonus: 200 },
  '渡劫期': { maxCultivation: 200000, levelBonus: 300 }
}

// 等级配置
const LEVEL_CONFIG = {
  baseHp: 100,
  baseMp: 80,
  hpPerLevel: 20,
  mpPerLevel: 10,
  attackPerLevel: 5,
  defensePerLevel: 3
}

export const usePlayerStore = defineStore('player', {
  state: () => ({
    // 基本信息
    basicInfo: {
      name: '青云子',
      level: 5,
      experience: 350,
      maxExperience: 500,
      gold: 1250
    },
    
    // 修为系统
    cultivation: {
      current: 1200,
      max: 2000,
      realm: '筑基期',
      stage: '初期'
    },
    
    // 战斗属性
    stats: {
      hp: 150,
      maxHp: 150,
      mp: 100,
      maxMp: 100,
      attack: 50,
      defense: 30,
      speed: 10
    },
    
    // 装备信息
    equipment: {
      weapon: null,
      armor: null,
      accessory: null
    },
    
    // 技能信息
    skills: [],
    
    // 状态效果
    statusEffects: []
  }),
  
  getters: {
    // 进度计算器
    progress: (state) => ({
      cultivation: Math.round((state.cultivation.current / state.cultivation.max) * 100),
      hp: Math.round((state.stats.hp / state.stats.maxHp) * 100),
      mp: Math.round((state.stats.mp / state.stats.maxMp) * 100),
      experience: Math.round((state.basicInfo.experience / state.basicInfo.maxExperience) * 100)
    }),
    
    // 完整境界显示
    fullRealm: (state) => `${state.cultivation.realm} ${state.cultivation.stage}`,
    
    // 总属性计算（包含装备加成）
    totalStats: (state) => {
      const baseStats = { ...state.stats }
      // 这里可以添加装备加成计算
      return baseStats
    },
    
    // 是否可以升级
    canLevelUp: (state) => state.basicInfo.experience >= state.basicInfo.maxExperience,
    
    // 是否可以突破境界
    canAdvanceRealm: (state) => {
      const currentRealm = REALMS[state.cultivation.realm]
      return state.cultivation.current >= state.cultivation.max && currentRealm
    }
  },
  
  actions: {
    // 基础信息操作
    updateName(name) {
      this.basicInfo.name = name
    },
    
    gainExperience(amount) {
      this.basicInfo.experience = Math.min(this.basicInfo.experience + amount, this.basicInfo.maxExperience)
    },
    
    spendGold(amount) {
      if (this.basicInfo.gold >= amount) {
        this.basicInfo.gold -= amount
        return true
      }
      return false
    },
    
    gainGold(amount) {
      this.basicInfo.gold += amount
    },
    
    // 修为操作
    gainCultivation(amount) {
      this.cultivation.current = Math.min(this.cultivation.current + amount, this.cultivation.max)
    },
    
    // 境界突破
    advanceRealm() {
      if (!this.canAdvanceRealm) return false
      
      const realms = Object.keys(REALMS)
      const currentIndex = realms.indexOf(this.cultivation.realm)
      
      if (currentIndex < realms.length - 1) {
        const nextRealm = realms[currentIndex + 1]
        const realmConfig = REALMS[nextRealm]
        
        this.cultivation.realm = nextRealm
        this.cultivation.stage = '初期'
        this.cultivation.max = realmConfig.maxCultivation
        this.cultivation.current = 0
        
        // 境界突破奖励
        this.basicInfo.level += Math.floor(realmConfig.levelBonus / 10)
        this.gainGold(realmConfig.levelBonus * 10)
        
        return true
      }
      return false
    },
    
    // 升级操作
    levelUp() {
      if (!this.canLevelUp) return false
      
      this.basicInfo.level++
      this.basicInfo.experience -= this.basicInfo.maxExperience
      
      // 更新最大经验值（随等级增长）
      this.basicInfo.maxExperience = Math.floor(this.basicInfo.maxExperience * 1.2)
      
      // 更新属性
      this.updateStats()
      
      return true
    },
    
    // 属性更新
    updateStats() {
      const level = this.basicInfo.level
      this.stats.maxHp = LEVEL_CONFIG.baseHp + (level * LEVEL_CONFIG.hpPerLevel)
      this.stats.maxMp = LEVEL_CONFIG.baseMp + (level * LEVEL_CONFIG.mpPerLevel)
      this.stats.attack = level * LEVEL_CONFIG.attackPerLevel
      this.stats.defense = level * LEVEL_CONFIG.defensePerLevel
      
      // 恢复满状态
      this.stats.hp = this.stats.maxHp
      this.stats.mp = this.stats.maxMp
    },
    
    // 战斗操作
    takeDamage(amount) {
      this.stats.hp = Math.max(this.stats.hp - amount, 0)
    },
    
    restoreHealth(amount) {
      this.stats.hp = Math.min(this.stats.hp + amount, this.stats.maxHp)
    },
    
    restoreMana(amount) {
      this.stats.mp = Math.min(this.stats.mp + amount, this.stats.maxMp)
    },
    
    // 装备操作
    equipItem(item, slot) {
      if (this.equipment[slot]) {
        // 卸下当前装备的逻辑可以在这里添加
      }
      this.equipment[slot] = item
      this.updateStats() // 重新计算属性
    },
    
    unequipItem(slot) {
      this.equipment[slot] = null
      this.updateStats() // 重新计算属性
    },
    
    // 重置玩家状态（用于测试或重新开始）
    resetPlayer() {
      this.basicInfo = {
        name: '青云子',
        level: 1,
        experience: 0,
        maxExperience: 100,
        gold: 100
      }
      this.cultivation = {
        current: 0,
        max: REALMS['练气期'].maxCultivation,
        realm: '练气期',
        stage: '初期'
      }
      this.updateStats()
    }
  }
})